﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Data;
using System.Globalization;
using System.Threading;
namespace CommonTagTime
{
    public static class clsConvert
    {
        public static Guid DbToGuid(string fieldName, IDataReader de)
        {
            try
            {
                object result=(object)de[fieldName];
                return new Guid(result.ToString());
            }
            catch
            {
            }
            return Guid.Empty;
        }
        public static float Float(object obj)
        {
            float i = float.MinValue;
            NumberFormatInfo num = (NumberFormatInfo)Thread.CurrentThread.CurrentCulture.NumberFormat.Clone();
            if (obj != null)
            {
                try
                {
                    i = Convert.ToSingle(obj, num);
                }
                catch
                {

                }
            }
            return i;
        }
        public static int Int(object obj)
        {
            int i = int.MinValue;
            if (obj != null)
            {
                try
                {
                    i = Convert.ToInt32(obj);
                }
                catch
                {

                }
            }
            return i;
        }
        public static byte Byte(object obj)
        {
            byte i = byte.MinValue;
            if (obj != null)
            {
                try
                {
                    i = Convert.ToByte(obj);
                }
                catch
                {

                }
            }
            return i;
        }

        public static double Double(object obj)
        {
            double d = double.MinValue;
            if (obj != null)
            {
                try
                {
                    d = Convert.ToDouble(obj);
                }
                catch
                {

                }
            }
            return d;
        }

        public static string String(object obj)
        {
            string str = string.Empty;
            if (obj != null)
            {
                str = obj.ToString();
            }
            return str;
        }
        public static string String(DateTime dtTime)
        {
            DateTimeFormatInfo nfi = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();               
            return dtTime.ToString(nfi.ShortDatePattern);
        }
        public static string String(DateTime dtTime,string format)
        {

            return dtTime.ToString(format);
        }
        public static string String(float _value)
        {

            if (_value < 0)
            {
                return string.Empty;
            }
            NumberFormatInfo nfi = (NumberFormatInfo)Thread.CurrentThread.CurrentCulture.NumberFormat.Clone();
            return _value.ToString("N", nfi);
        }
        public static bool Boolean(object obj)
        {
            try
            {
                return (bool)obj;
            }
            catch
            {
            }
            return false;
        }
        public static DateTime Date(object obj)
        {
            DateTimeFormatInfo nfi = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();
            DateTime date = DateTime.MinValue;
            if (obj != null)
            {
                try
                {
                    date = Convert.ToDateTime(obj,nfi);
                }
                catch
                {

                }
            }
            return date;
        }
        /***-----convert value insert DB-------****/
        public static object GuidToDB(Guid value)
        {
            if (value == Guid.Empty)
                return null;
            return value;
        }
        public static object IntToDB(int value)
        {
            if (value == Int32.MinValue)
                return null;
            return value;
        }
        public static object ByteToDB(byte value)
        {
            if (value == byte.MinValue)
                return null;
            return value;
        }
        public static object Int64ToDB(Int64 value)
        {
            if (value == Int64.MinValue)
                return null;
            return value;
        }
        public static object FloatToDB(float value)
        {
            if (value == float.MinValue)
                return null;
            return value;
        }
        public static object StringToDB(string value)
        {
            if (value == string.Empty)
                return DBNull.Value;
            return value;
        }
        public static object DateToDB(DateTime value)
        {
            if (value == DateTime.MinValue)
                return null;
            return value;
        }
        /***-----convert value from DB -------****/
        public static byte DbToByte(string fieldName, IDataReader de)
        {
            try
            {
                return clsConvert.Byte(de[fieldName]);
            }
            catch
            {
            }
            return byte.MinValue;
        }
        public static int DbToInt(string fieldName, IDataReader de)
        {
            try
            {
                return clsConvert.Int(de[fieldName]);
            }
            catch
            {
            }
            return Int32.MinValue;
        }
        public static float DbToFloat(string fieldName, IDataReader de)
        {
            try
            {
                return clsConvert.Float(de[fieldName]);
            }
            catch
            {
            }
            return float.MinValue;
        }
        public static string DbToString(string fieldName, IDataReader de)
        {
            string str = string.Empty;
            try
            {
                str =  (string)de[fieldName];
            }
            catch(Exception ex)
            {
                str = string.Empty;
            }
            return str;
        }
        public static DateTime DbToDateTime(string fieldName, IDataReader de)
        {
            try
            {
                return clsConvert.Date(de[fieldName]);
            }
            catch
            {
            }
            return DateTime.MinValue;
        }
        public static Boolean DbToBoolean(string fieldName, IDataReader de)
        {
            try
            {
                return clsConvert.Boolean(de[fieldName]);
            }
            catch
            {
            }
            return false;
        }
    }
}
